<?php
/**
 * Redis过期订阅.
 * User: 1874
 */
namespace app\commom\controller;
use app\BaseController;
use think\facade\Cache;

class Subscribe extends BaseController{
    protected $ip = '127.0.0.1';
    protected $port = 6379;
    /*
     * 开启订阅
     * 命令行调用 php index.php common/subscribe/psubscribe
     */
    public function psubscribe(){
        $redis = new \Redis();
        $redis->connect($this->ip, $this->port);
        //解决Redis客户端订阅时候超时情况
        $redis->setOption(\Redis::OPT_READ_TIMEOUT, -1);
        $redis->psubscribe(array('__keyevent@0__:expired'), 'keyCallback');
    }
    /*
     * 设置过期redis值
     */
    public function setRedis(){
        $order = 'orderId-1001011-db-adminplatform-filed-ad_id';
        $redis = Cache::handler();
        if($redis->get($order)){
            dump($redis->get($order));
        }else{
            dump($redis->setex($order, 5, 1));
        }
    }
    /*
     * 回调方法需写在 common.php 中
     */
    /*function keyCallback($redis, $pattern, $chan, $msg){
        echo "Pattern: $pattern\n";
        echo "Channel: $chan\n";
        echo "Payload: $msg\n";
        //keyCallback为订阅事件后的回调函数，这里写业务处理逻辑，
    }*/
}